package cn.javabb.core.service.blog;

import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseService;
import cn.javabb.core.entity.blog.BlogTypeDO;
import cn.javabb.core.mapper.blog.BlogTypeMapper;
import cn.javabb.core.model.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/12/01 18:19
 */
@Service
public class BlogTypeService extends BaseService<BlogTypeDO> {
    @Autowired
    private BlogTypeMapper blogTypeMapper;
    @Autowired
    private BlogService blogService;

    public List<BlogTypeDO> listBlogTypes(){
        BlogTypeDO type = new BlogTypeDO().setState(StatusEnum.NORMAL.getCode());
        return this.queryListByWhere(type);
    }

    public List<BlogTypeDO> listWithBlogTotal(){
        return blogTypeMapper.listWithBlogTotal();
    }

    /**
     * 更新状态
     * @param ids
     * @param state
     * @return
     */
    public boolean updateState(String ids, Integer state) {
        List<String> idList = ListUtils.stringToListString(ids);
        Example ex = new Example(BlogTypeDO.class);
        ex.createCriteria().andIn("id", idList);
        BlogTypeDO bt = new BlogTypeDO();
        bt.setState(state);
        return this.blogTypeMapper.updateByExampleSelective(bt, ex)>0?true:false;
    }

    /**
     * 删除类别
     * @param ids
     * @return
     */
    @Transactional
    public boolean delete(String ids) {
        List<String> idList = ListUtils.stringToListString(ids);
        int num = 0;
        for (String id : idList) {
            BlogTypeDO blog = this.queryById(id);
            // 删除博客类别
            num += this.deleteById(id);
            // 类别下的博客
            blogService.deleteByTypeId(Integer.valueOf(id));
        }
        return num>0?true:false;
    }
}
